package org.changchen.fgt.service.impl;

import java.io.File;
import java.util.ArrayList;
import java.util.Comparator;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

import org.changchen.fgt.config.SourceApiConfig;
import org.changchen.fgt.dto.ApiInfo;
import org.changchen.fgt.service.SourceTestService;
import org.changchen.fgt.utils.UrlUtil;
import org.springframework.beans.factory.config.YamlMapFactoryBean;
import org.springframework.boot.system.ApplicationHome;
import org.springframework.core.io.ClassPathResource;
import org.springframework.core.io.PathResource;
import org.springframework.stereotype.Service;

import com.alibaba.fastjson.JSON;

/**   
* @ClassName:  SourceTestServiceImpl   
* @Description:采集资源测试服务   
* @author:     ChangChen 
* @date:       2024-03-26 08:45
* @Copyright:  2024 ChangChen. All rights reserved. 
*/
@Service
public class SourceTestServiceImpl implements SourceTestService{
	
	
	
	/**   
	 * @Title:      sourceList   
	 * @Description:资源列表   
	 * @param:      @return      
	 * @return:     List<ApiInfo>      
	 * @throws   
	 */
	@Override
	public List<ApiInfo> sourceList(){
		
		// 配置文件例的资源
		List<ApiInfo> sourceList = this.yamlSourceList();
		
		// 每个资源的有效情况
		for(ApiInfo source : sourceList) {
			String code = source.getCode();
			ApiInfo apiInfo = SourceApiConfig.SRC_CONFIG.get(code);
			source.setActive(apiInfo != null);
		}

		return sourceList.stream()
				.sorted(Comparator.comparing(ApiInfo::getActive).thenComparing(ApiInfo::getCode))
				.collect(Collectors.toList());
	}
	
	
	
	/**   
	 * @Title:      sourceTest   
	 * @Description:资源测试   
	 * @param:      @param url
	 * @param:      @return      
	 * @return:     Boolean      
	 * @throws   
	 */
	@Override
	public Boolean sourceTest(String code) {
		ApiInfo apiInfo = this.yamlSourceOne(code);
		// 测试url以搜索接口为主
		String testUrl = apiInfo.getUrl() + "?ac=list&wd=三国";
		return UrlUtil.testUrl(testUrl);
	}
	
	
	/**   
	 * @Title:      sourceAdd   
	 * @Description:新增资源   
	 * @param:      @param code
	 * @param:      @return      
	 * @return:     Boolean      
	 * @throws   
	 */
	@Override
	public Boolean sourceAdd(String code) {
		ApiInfo apiInfo = this.yamlSourceOne(code);
		apiInfo.setActive(true);
		SourceApiConfig.SRC_CONFIG.put(code, apiInfo);
		return true;
	}
	
	
	/**   
	 * @Title:      sourceDelete   
	 * @Description:资源删除   
	 * @param:      @param code
	 * @param:      @return      
	 * @return:     Boolean      
	 * @throws   
	 */
	@Override
	public Boolean sourceDelete(String code) {
		SourceApiConfig.SRC_CONFIG.remove(code);
		return true;
	}
	
	
	private ApiInfo yamlSourceOne(String code) {
		
		// 通过 getObject() 方法获取 Map 对象	
		Map<String,Object> srcMap = this.yamlSourceMap();
		ApiInfo apiInfo = JSON.parseObject(JSON.toJSONString(srcMap.get(code)), ApiInfo.class);
		apiInfo.setCode(code);
		return apiInfo;
	}

	
	private List<ApiInfo> yamlSourceList(){
	
		// 通过 getObject() 方法获取 Map 对象	
		Map<String,Object> srcMap = this.yamlSourceMap();
		List<ApiInfo> resultList = new ArrayList<>();
		
		srcMap.entrySet().stream().forEach(item -> {
			ApiInfo apiInfo = JSON.parseObject(JSON.toJSONString(item.getValue()), ApiInfo.class);
			apiInfo.setCode(item.getKey());
			resultList.add(apiInfo);
		});
		
		return resultList;
		
	}
	
	
	private Map<String,Object> yamlSourceMap(){
		
		String fileName = SourceApiConfig.CONFIG_FILE;
		YamlMapFactoryBean yamlMapFactoryBean = new YamlMapFactoryBean();
		
		
		// 外部配置文件
		String yamlFile = this.getParentDir() + File.separator + fileName;
		File extFile = new File(yamlFile);
		if(!extFile.exists()) {
			// 内置配置文件	
			ClassPathResource resource = new ClassPathResource(fileName);
			// 加载 Yaml 文件
			yamlMapFactoryBean.setResources(resource);
		} else {
			yamlMapFactoryBean.setResources(new PathResource(yamlFile));
		}
		
		// 通过 getObject() 方法获取 Map 对象	
		return yamlMapFactoryBean.getObject();
		
	}

	/**   
	 * @Title:      getParentDir   
	 * @Description:获取 Jar 所在目录   
	 * @param:      @return      
	 * @return:     String      
	 * @throws   
	 */
	private String getParentDir() {
		// 获取 jar 所在目录
		ApplicationHome home = new ApplicationHome(this.getClass());
		File jarFile = home.getSource();
		return jarFile.getParentFile().toString();
	}
	
	
}
